/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.member;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;
import model.UserService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author SONY
 */
public class LoginStrutsAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS_MEMBER = "success_member";
    private static final String SUCCESS_ADMIN = "success_admin";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            //store user object in the session-scope
            HttpSession session = request.getSession();
            if((session.getAttribute("user_member") == null) || (session.getAttribute("user_admin") == null)) {
                String email = request.getParameter("email");
                String password = request.getParameter("password");
                String remember = request.getParameter("remember");
                UserService loginService = new UserService();
                User user = loginService.checkLogin(email, password);
                if (user != null) {
                    //Remember Account
                    int expiry = -1;
                    if ((remember != null) && (remember.equals("checked"))) {
                        expiry = 60 * 60 * 24 * 7; // 7 days
                    }
                    Cookie email_cookie = new Cookie("email", email);
                    Cookie password_cookie = new Cookie("password", password);
                    Cookie remember_cookie = new Cookie("remember", remember);
                    email_cookie.setMaxAge(expiry);
                    password_cookie.setMaxAge(expiry);
                    remember_cookie.setMaxAge(expiry);
                    response.addCookie(email_cookie);
                    response.addCookie(password_cookie);
                    response.addCookie(remember_cookie);
                    if(user.getMemberID() == 0) {
                        session.setAttribute("user_admin", user);
                        return mapping.findForward(SUCCESS_ADMIN);
                    } else {
                        session.setAttribute("user_member", user);
                        return mapping.findForward(SUCCESS_MEMBER);
                    }
                }
                if(session.getAttribute("user_member") != null) {
                    return mapping.findForward(SUCCESS_MEMBER);
                }
                if(session.getAttribute("user_admin") != null) {
                    return mapping.findForward(SUCCESS_ADMIN);
                }
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        request.setAttribute("errorMsg", "Email hoặc Mật Khẩu không đúng! Hãy kiểm tra lại.");
        return mapping.findForward("login_member");
    }
}
